﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace Sustainalytics.ReportGenerator.BL
{
    public class IncidentsChart
    {
        public SeriesPoints Incidents { get; set; }
        public SeriesPoints MajorIncident { get; set; }

        public byte[] GetChartImage(int width, int height)
        {
            using (var chartTemplate = new IncidentsChartTemplate(width, height))
            {

                if (Incidents != null)
                {
                    chartTemplate.Series[0].Points.Clear();

                    for (var i = 0; i < Incidents.XValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = Incidents.XValues[i],
                            YValues = new[] {Incidents.YValues[i], 0}
                        };
                        chartTemplate.Series[0].Points.Add(dataPoint);
                    }
                }

                if (MajorIncident != null && MajorIncident.XValues != null && MajorIncident.YValues != null)
                {
                    chartTemplate.Series[1].Points.Clear();

                    for (var i = 0; i < MajorIncident.XValues.Count(); i++)
                    {
                        var dataPoint = new Dundas.Charting.WinControl.DataPoint
                        {
                            XValue = MajorIncident.XValues[i],
                            YValues = new[] {MajorIncident.YValues[i], 0}
                        };
                        chartTemplate.Series[1].Points.Add(dataPoint);
                    }
                }

                // Create new bitmap where chart will be printed
                using (var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb))
                {
                    // Create graphics object from bitmap.
                    using (var imageGraphics = Graphics.FromImage(bitmap))
                    {
                        // Print chart to the bitmap object.
                        var rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                        //todo: petre -  ugly hack  to avoid pdf failures
                        int trials = 5;
                        do
                        {
                            try
                            {
                                chartTemplate.Printing.PrintPaint(imageGraphics, rectangle);
                                trials = 0;
                            }
                            catch
                            {
                                System.Threading.Thread.Sleep(500);
                                trials--;
                            }
                        }
                        while (trials > 0);
                    }

                    using (var stream = new MemoryStream())
                    {
                        bitmap.Save(stream, ImageFormat.Png);
                        var buffer = stream.ToArray();
                        return buffer;
                    }
                }
            }
        }
    }
}
